// index.js
// 获取应用实例da
const db = wx.cloud.database({
  env:'xxsc-5gtn9oa3fe293cf0',
})
const App = getApp();//设立顶部栏高度
var common3 = require('data3.js')
var common2 = require('data2.js')
var common1 = require('data1.js');
const data3 = require('./data3');
Page({
  data: {
    navId: 2, //导航栏id
    currentId: 2, //swiper下标
    swipH: 399,
    mainHt: 0,
    num:12,
    numfor:12,
    data3:{},
  },
  onShow: function() {
    var that=this;

    db.collection('data1').doc('list1').get({ // .get() 获取集合 / 记录数据    var datas=datas;
      
      success: function(res) {

        that.setData({
          data1:res.data,
          
        })
        // res.da ta 包含该记录的数据
      }
      
    }),
    db.collection('data2').doc('list1').get({ // .get() 获取集合 / 记录数据    var datas=datas;
      success: function(res) {
        that.setData({
          data2:res.data,
          
        })
        // res.da ta 包含该记录的数据
      }
      
    })

    //设置数据 只接收json数据类型
  },
  topSwiper(event) {
    var that=this;
    let navId = event.detail.current; //获取swiper下标
    console.log(navId);
    this.setData({
      navId,
      currentId: navId,
    })
    console.log('last'+navId);
    if(navId==2){
      var action=that.action;
      action();
    }
    if(navId==2&&!App.globalData.hasUserInfo){
      wx.switchTab({
        url: '../collect/collect'   
        });
    }
  },
  //导航栏
  changNav(event) {
  
    let navId = event.currentTarget.dataset.id; //获取导航栏下标
    if (this.data.currentId == navId) {
      return false;
    } else {
      this.setData({
        currentId: navId,
        navId,
      })
  }
  },
  onLoad: function (options) {
    this.getOpenid();
    this.setData({
      data3:common3.data3,
      IsDrop:data3.IsDrop
    })
    //获取屏幕高度
    let that = this;
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    let ratio = 750 / windowWidth;
    wx.getSystemInfo({
      success: function (res) {
        // 获取可使用窗口宽度
        let clientHeight = res.windowHeight;
        // 获取可使用窗口高度
        let clientWidth = res.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = (clientHeight-121) * ratio;
        // 设置高度
        App.globalData.allheight=clientHeight*ratio;
        that.setData({
          mainHt: height
        });
      }
    });
    this.setData({
      navH: App.globalData.navHeight+60,
      windowWidth: windowWidth*0.7,
      scroll_height: (windowHeight-121)*ratio,
      //App.globalData.navHeight=43
    });
  },
  jump(){
    wx.navigateTo({
      url: '../new/new',
    })
  },
  //点击进入搜索页面
 bindFocus: function () {
   wx.redirectTo({
     url: '../new/new',
   })
},
tuij(){
  wx.navigateTo({
    url: 'pages/new/new',
  })
},
updata(){
  let numfor = this.data.num+12;
  var that=this;
  that.setData({
    num:numfor
  });
},
despmore(){
  wx.navigateTo({
    url: '../thing/thing',
  })
},
IsExist:function(){
  var that=this;
  var getOpenid=App.globalData.openid;
  db.collection('users').get({
    success: function(res) {
      // res.data 是一个包含集合中有权限访问的所有记录的数据，不超过 20 条
      var arry=[];
      arry=res.data;
      for(var i=0;i<arry.length;i++){
        if(arry[i]._openid==getOpenid){
           App.globalData.hasUserInfo=true;
           App.globalData.userData=arry[i];

        }
      }
    }
  })
},
 getOpenid:function() {
  wx.cloud.callFunction({
   name: 'getOpenid',
   complete: res => {
    App.globalData.openid=res.result.openId;
    console.log(res.result.openId);
    this.IsExist();
   }
  })
 },
 forDrop:function () {
   var data3=this.data.data3;
   var that=this;
   data3.IsDrop=!data3.IsDrop;
   data3.show=!data3.show;
   this.setData({
     data3:data3,
   })
 },
 forChange:function (event) {
  var that=this;
  var data3=this.data.data3;
  var navId = event.currentTarget.dataset.id;
  if(data3.currentId!=navId){
    data3.currentId=navId;
    data3.show=!data3.show;
    data3.IsDrop=!data3.IsDrop;
    that.setData({
      data3:data3,
    })
  }
},
    textChange:function (event) {
      var that=this;
      var vary=event.detail.scrollTop;
      if(vary<=20){
      var now=20-vary;
      var precent=now/20;
      var num=8*precent+16;
      var margin=0-vary*1.4;
      var opacity=1-vary/20;
      var near=90-45*vary/20;
      // 28/20
      that.setSize(num,margin,opacity,near)
      }else{
        that.setSize(16,-28,0,45)
      }

      //console.log(event); 
    },
    setSize(num,margin,opacity,near){
      //event.detail.scrollTop=0-20;
      //24-16
      var that=this;
      var data3=this.data.data3;
      data3.titleSize=num;
      data3.marginfor=margin;
      data3.opacity=opacity;
      data3.nearfor=near;
      this.setData({
        data3:data3,
      })
    }
})
